package com.baitiaojun.common.utils.file;

import cn.hutool.core.util.IdUtil;
import com.baitiaojun.common.enums.impl.ServiceExpEnums;
import com.baitiaojun.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class FileUtils {

    /**
     * 根据file对象获取文件类型
     * @param file
     * @return
     */
    public static String getFileType(File file) {
        return getFileType(file.getName());
    }

    /**
     * 根据名称获取文件类型
     * @param fileName
     * @return
     */
    public static String getFileType(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return StringUtils.EMPTY;
        }
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
        if (StringUtils.isEmpty(fileType)) {
            return StringUtils.EMPTY;
        }
        return fileType.toLowerCase();
    }

    /**
     * 验证文件名是否有效
     * @param fileName
     * @return
     */
    public static boolean isValid(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return false;
        }
        return fileName.matches("[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+");
    }

    public static long getFileSize(String filePath) {
        long size;
        try {
            size = Files.size(Paths.get(filePath));
        } catch (IOException e) {
            log.warn("文件大小获取失败");
            return 0;
        }
        return size;
    }

    /**
     * 根据旧的文件名重命名文件
     * @param originalFileName
     * @return
     */
    public static String createFileName(String originalFileName) {
        String fileType = getFileType(originalFileName);
        if (StringUtils.isEmpty(fileType)) {
            return StringUtils.EMPTY;
        }
        String fileName = IdUtil.simpleUUID() + "." + fileType;
        if (!isValid(fileName)) {
            throw new ServiceException(ServiceExpEnums.EXP_FILE_NAME_INVALID);
        }
        return fileName;
    }

    /**
     * 创建指定的多级目录
     * @param directoryPath
     * @return
     */
    public static String createFileDir(String directoryPath) {
        Path path = Paths.get(directoryPath);
        try {
            if (Files.notExists(path)) {
                path = Files.createDirectories(path);
            }
        } catch (IOException e) {
            log.warn("文件创建失败");
            return StringUtils.EMPTY;
        }
        return path.toAbsolutePath().toString();
    }

    /**
     * 删除指定路径的目录
     * @param directoryPath
     * @return
     */
    public static boolean deleteFileDir(String directoryPath) {
        boolean isTrue;
        try {
            isTrue = Files.deleteIfExists(Paths.get(directoryPath));
        } catch (IOException e) {
            log.warn("文件删除失败");
            return false;
        }
        return isTrue;
    }

    /**
     * 检查是否是允许的文件类型
     * @param suffix
     * @param types
     * @return
     */
    public static boolean isAllowedType(String suffix, String[] types) {
        for (String type : types) {
            if (suffix.equalsIgnoreCase(type)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取文件列表
     * @param directoryPath
     * @return
     */
    public static List<String> getFileList(String directoryPath) {
        List<String> fileList = new ArrayList<>();
        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(directoryPath))) {
            for (Path path : directoryStream) {
                if (Files.isRegularFile(path)) {
                    fileList.add(path.getFileName().toString());
                }
            }
        } catch (IOException e) {
            log.warn("获取文件列表失败");
            return new ArrayList<>();
        }
        return fileList;
    }

//    public String getPropertyValue(String ) {
//
//    }
}
